#include "bsp_adc.h"
#include "systick.h"
#include "bsp_usart.h"
#include "bsp_gpio.h"
#include "bsp.h"
static uint16_t adc_value[ADC_DMA_NUM_MAX];
 
const tsADCChannel ADCChannelTable[ADC_NUM_MAX]={	
#if (DEV_HARD_VER<=10001) 	
		ADC_CHANNEL_SODA_H	,								/*苏达水*/
#else
		ADC_CHANNEL_SODA_L	,								/*苏达水*/
		ADC_CHANNEL_SODA_H	,								/*苏达水*/
#endif		
	
	ADC_CHANNEL_COOL_L	,								/*冷罐低水位*/		
	ADC_CHANNEL_COOL_H	,								/*冷罐高水位*/
	ADC_CHANNEL_HEAT_L	,								/*热罐低水位*/		
	ADC_CHANNEL_HEAT_M	,								/*热罐中水位*/	
	ADC_CHANNEL_HEAT_H	,								/*热罐高水位*/  
};	

const tsADCChannel ADCDMAChannelTable[ADC_DMA_NUM_MAX]={	
	ADC_CHANNEL_HEAT_NTC,			         	/*热罐NTC*/
	ADC_CHANNEL_COOL_NTC,			          /*冷罐NTC*/
	ADC_CHANNEL_SODA_NTC,			          /*苏达NTC*/
	ADC_CHANNEL_HEAT_NTC_L,							/*热罐下NTC*/
	ADC_CHANNEL_IN_WATER_NTC,			      /*进水NTC 暂时不用*/ 
	
	
	ADC_CHANNEL_STIRRER_PUMP_CUR,				/*搅拌泵电流*/
	ADC_CHANNEL_HOT_PUMP_CUR,						/*热水泵电流*/
	ADC_CHANNEL_SODA_PUMP_CUR,					/*苏达泵电流*/
	ADC_CHANNEL_FAN_CUR,								/*风扇电流*/
};

uint16_t OnReadADCDMAvalue(uint32_t channle)
{
		/******************DMA**********************/		
	  return adc_value[channle];
}

uint16_t OnReadADCvalue(uint32_t channle)
{	
/*****************非DMA**************TDS检测使用**********/	 	
	uint64_t tempTick = HAL_GetTickMs();

	ADC_ConfigRegularChannel(ADC2, ADCChannelTable[channle].channel, 1, ADC_SAMPLETIME_13CYCLES5);
	
	ADC_EnableSoftwareStartConv(ADC2);
	
	while(!ADC_ReadStatusFlag(ADC2, ADC_FLAG_EOC) && ((HAL_GetTickMs() - tempTick) < 10));//最长时间10ms超时
	/* clear the end of conversion flag */
	ADC_ClearStatusFlag(ADC2, ADC_FLAG_EOC);
	return ADC_ReadConversionValue(ADC2);
}

void MX_ADC_Init(void)
{
//兆易
//	 /* enable GPIOC clock */
//    rcu_periph_clock_enable(RCU_GPIOC);
	  RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_GPIOC);
//	 /* enable ADC0 clock */
//    rcu_periph_clock_enable(RCU_ADC0);  //用于非TDS类  采样DMA模式
//		rcu_periph_clock_enable(RCU_ADC1);  //用于TDS类
	  RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_ADC1);
    RCM_EnableAPB2PeriphClock(RCM_APB2_PERIPH_ADC2);
//	/* config ADC clock */
//    rcu_adc_clock_config(RCU_CKADC_CKAPB2_DIV6);
	  RCM_ConfigADCCLK(RCM_PCLK2_DIV_6);
//	 /* enable DMA clock */
//    rcu_periph_clock_enable(RCU_DMA0);
	  RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_DMA1);

	 /* config the GPIO as analog mode */  
//NTC
	  gpio_init(ADC_NTC_HEAT_GPIO_Port, 								GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_NTC_HEAT_Pin);           		//热水温度NTC
		gpio_init(ADC_NTC_COOL_GPIO_Port, 								GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_NTC_COOL_Pin);							//冷罐温度NTC
	  gpio_init(ADC_NTC_SODA_GPIO_Port, 								GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_NTC_SODA_Pin);							//苏达温度NTC
		
		gpio_init(ADC_NTC_HEAT_L_GPIO_Port, 							GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_NTC_HEAT_L_Pin);						//热罐下ADC
		gpio_init(ADC_NTC_IN_WATER_GPIO_Port, 						GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_NTC_IN_WATER_Pin);					//进水NTC ADC
		

		
//TDS
#if (DEV_HARD_VER<=10001) 	
		gpio_init(ADC_SODA_H_TDS_GPIO_Port,       				GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_SODA_H_TDS_Pin);				 //苏达高水位ADC 
#else
		gpio_init(ADC_SODA_L_TDS_GPIO_Port,       				GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_SODA_L_TDS_Pin);				 //苏达高水位ADC 
		gpio_init(ADC_SODA_H_TDS_GPIO_Port,       				GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_SODA_H_TDS_Pin);				 //苏达高水位ADC 
#endif	
		gpio_init(ADC_SODA_H_TDS_GPIO_Port,       				GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_SODA_H_TDS_Pin);				 //苏达高水位ADC 
		gpio_init(ADC_COOL_L_TDS_GPIO_Port, 							GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_COOL_L_TDS_Pin);				 //冷罐低水位ADC
		gpio_init(ADC_COOL_H_TDS_GPIO_Port, 							GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_COOL_H_TDS_Pin);				 //冷罐高水位ADC  
		gpio_init(ADC_HEAT_L_TDS_GPIO_Port, 							GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_HEAT_L_TDS_Pin);				 //热罐低水位ADC 
		gpio_init(ADC_HEAT_M_TDS_GPIO_Port, 							GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_HEAT_M_TDS_Pin);				 //热罐中水位ADC 
		gpio_init(ADC_HEAT_H_TDS_GPIO_Port, 							GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_HEAT_H_TDS_Pin);				 //热罐高水位ADC
//电流
		gpio_init(ADC_HOT_PUMP_GPIO_Port, 								GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_HOT_PUMP_Pin);				 	//热水泵ADC
		gpio_init(ADC_SODA_PUMP_GPIO_Port, 								GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_SODA_PUMP_Pin);				   //苏达泵ADC
		gpio_init(ADC_FAN_GPIO_Port, 											GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_FAN_Pin);				   			//风扇ADC
		gpio_init(ADC_STIRRER_GPIO_Port, 									GPIO_MODE_AIN, 		GPIO_OSPEED_MAX, 		ADC_STIRRER_Pin);				   //搅拌泵ADC

//		adc_software_trigger_enable(ADC0, ADC_REGULAR_CHANNEL);
		ADC_Config_T            adcConfig;
	
    /* ADC configuration */
    ADC_Reset(ADC1);

    ADC_ConfigStructInit(&adcConfig);
    adcConfig.mode                  = ADC_MODE_INDEPENDENT;
    adcConfig.scanConvMode          = ENABLE;
    adcConfig.continuosConvMode     = ENABLE;
    adcConfig.externalTrigConv      = ADC_EXT_TRIG_CONV_None;
    adcConfig.dataAlign             = ADC_DATA_ALIGN_RIGHT;
    /* channel number */
    adcConfig.nbrOfChannel          = ADC_DMA_NUM_MAX;
    ADC_Config(ADC1, &adcConfig);

    /* ADC channel Convert configuration */
    for(uint8_t i=0;i<ADC_DMA_NUM_MAX; i++ )
		ADC_ConfigRegularChannel(ADC1, ADCDMAChannelTable[i].channel, i+1, ADC_SAMPLETIME_239CYCLES5);
//		ADC_ConfigRegularChannel(ADC1, ADCDMAChannelTable[0].channel, 1, ADC_SAMPLETIME_13CYCLES5);

    /* Enable ADC DMA */
    ADC_EnableDMA(ADC1);

    /* Enable ADC */
    ADC_Enable(ADC1);
	
    /* Enable ADC1 reset calibration register */
    ADC_ResetCalibration(ADC1);
    /* Check the end of ADC1 reset calibration register */
    while (ADC_ReadResetCalibrationStatus(ADC1));

    /* Start ADC1 calibration */
    ADC_StartCalibration(ADC1);
    /* Check the end of ADC1 calibration */
    while (ADC_ReadCalibrationStartFlag(ADC1));
	
		
    /* ADC configuration */
    ADC_Reset(ADC2);

    ADC_ConfigStructInit(&adcConfig);
    adcConfig.mode                  = ADC_MODE_INDEPENDENT;
    adcConfig.scanConvMode          = ENABLE;
    adcConfig.continuosConvMode     = DISABLE;
    adcConfig.externalTrigConv      = ADC_EXT_TRIG_CONV_None;
    adcConfig.dataAlign             = ADC_DATA_ALIGN_RIGHT;
    /* channel number */
    adcConfig.nbrOfChannel          = 1;
    ADC_Config(ADC2, &adcConfig);

    /* ADC channel Convert configuration */
//    for(uint8_t i=0;i<ADC_NUM_MAX; i++ )
//		ADC_ConfigRegularChannel(ADC2, ADCChannelTable[i].channel, i+1, ADC_SAMPLETIME_13CYCLES5);

    /* Enable ADC DMA */
//    ADC_EnableDMA(ADC2);

    /* Enable ADC */
    ADC_Enable(ADC2);
		
    /* Enable ADC1 reset calibration register */
    ADC_ResetCalibration(ADC2);
    /* Check the end of ADC1 reset calibration register */
    while (ADC_ReadResetCalibrationStatus(ADC2));

    /* Start ADC1 calibration */
    ADC_StartCalibration(ADC2);
    /* Check the end of ADC1 calibration */
    while (ADC_ReadCalibrationStartFlag(ADC2));
/****************DMA***************************************/
		DMA_Config_T dmaConfig;

    /* Enable DMA Clock */
    RCM_EnableAHBPeriphClock(RCM_AHB_PERIPH_DMA1);

    /* DMA config */
    dmaConfig.peripheralBaseAddr = ((uint32_t)ADC1_BASE + 0x4C);
    dmaConfig.memoryBaseAddr = (uint32_t)&adc_value;
    dmaConfig.dir = DMA_DIR_PERIPHERAL_SRC;
    dmaConfig.bufferSize = ADC_DMA_NUM_MAX;
    dmaConfig.peripheralInc = DMA_PERIPHERAL_INC_DISABLE;
    dmaConfig.memoryInc = DMA_MEMORY_INC_ENABLE;
    dmaConfig.peripheralDataSize = DMA_PERIPHERAL_DATA_SIZE_HALFWORD;
    dmaConfig.memoryDataSize = DMA_MEMORY_DATA_SIZE_HALFWORD;
    dmaConfig.loopMode = DMA_MODE_CIRCULAR;
    dmaConfig.priority = DMA_PRIORITY_HIGH;
    dmaConfig.M2M = DMA_M2MEN_DISABLE;

    /* Enable DMA channel */
    DMA_Config(DMA1_Channel1, &dmaConfig);

//	DMA_EnableInterrupt(DMA1_Channel1, DMA_INT_TC);
//	
//    NVIC_EnableIRQRequest(DMA1_Channel1_IRQn, 2, 0);
	
    /* Enable DMA */
    DMA_Enable(DMA1_Channel1);
	
    ADC_EnableSoftwareStartConv(ADC1);

}



void test_adc(void)
{
//	UV_LIGHT_ON();
//	print_debug("ADC_I_UV_LIGHT: %d \r\n",OnReadADCDMAvalue(ADC_I_UV_LIGHT));  // 0.32v  401  OFF 0v  0
//	UV_LIGHT_OFF();
	print_debug("ADC_NTC_COOL: %d \r\n",OnReadADCDMAvalue(ADC_NTC_COOL));
//	print_debug("ADC_NTC_PURE: %d \r\n",OnReadADCDMAvalue(ADC_NTC_PURE));
//	print_debug("ADC_NTC_NOR1: %d \r\n",OnReadADCDMAvalue(ADC_NTC_NOR1));
//	print_debug("ADC_NTC_HOT1: %d \r\n",OnReadADCDMAvalue(ADC_NTC_HOT1));
//	print_debug("ADC_NTC_NOR2: %d \r\n",OnReadADCDMAvalue(ADC_NTC_NOR2));
//	print_debug("ADC_NTC_HOT2: %d \r\n",OnReadADCDMAvalue(ADC_NTC_HOT2));
//		print_debug("ADC_I_UV1_LIGHT: %d \r\n",OnReadADCDMAvalue(ADC_I_UV1_LIGHT));
//	  print_debug("ADC_I_UV2_LIGHT: %d \r\n",OnReadADCDMAvalue(ADC_I_UV2_LIGHT));
//	  NORMAL_WATER_BUMP_PWM_PLUSE(100);
//	print_debug("ADC_I_NORMAL_PUMP: %d \r\n",OnReadADCDMAvalue(ADC_I_NORMAL_PUMP));
//	print_debug("ADC_I_HEAT_PUMP: %d \r\n",OnReadADCDMAvalue(ADC_I_HEAT_PUMP));

//		HAL_GPIO_WritePin(HEAT_L_TDS_FWD_GPIO_Port,		  HEAT_L_TDS_FWD_Pin,	  GPIO_PIN_SET); 
//		HAL_GPIO_WritePin(HEAT_M_TDS_FWD_GPIO_Port,		  HEAT_M_TDS_FWD_Pin,	  GPIO_PIN_SET);
//		HAL_GPIO_WritePin(HEAT_H_TDS_FWD_GPIO_Port ,		HEAT_H_TDS_FWD_Pin,	  GPIO_PIN_SET);
//		HAL_GPIO_WritePin(HEAT_COM_TDS_FWD_GPIO_Port,		HEAT_COM_TDS_FWD_Pin,	GPIO_PIN_RESET);    //高中低共用一个探针
//	print_debug("ADC_TDS_HEAT_L: %d \r\n",OnReadADCvalue(ADC_TDS_HEAT_L));
//	print_debug("ADC_TDS_HEAT_M: %d \r\n",OnReadADCvalue(ADC_TDS_HEAT_M));
//	print_debug("ADC_TDS_HEAT_H: %d \r\n",OnReadADCvalue(ADC_TDS_HEAT_H));
	
//	uint16_t buf[ADC_NUM_MAX];
////	print_debug("I_HEAT_PUMP: %d \r\n",OnReadADCvalue(ADC_I_HEAT_PUMP));//热水泵
////	print_debug("I_NORMAL_PUMP: %d \r\n",OnReadADCvalue(ADC_I_NORMAL_PUMP));//常温水泵
//	
//  for(uint8_t i=0;i<ADC_NUM_MAX; i++)
//	{
//		 buf[i]=OnReadADCvalue(i);
//		print_debug("buf[%d]: %d \r\n",i,buf[i]);
//		float vref_value = buf[i] * 3.3 / 4096;
//    
//		switch(i)
//		{
//			case ADC_NTC_HEAT:    print_debug("NTC_HEAT: %f \r\n",vref_value);    
//			break;
//			case ADC_NTC_PURE:		print_debug("NTC_PURE: %f \r\n",vref_value); 
//			break;
//			case ADC_NTC_WATER_OUT:print_debug("NTC_WATER_OUT: %f \r\n",vref_value); 
//			break;
//			case ADC_TDS_OVER_WATER:print_debug("TDS_OVER_WATER: %f \r\n",vref_value); 
//			break;
//			case ADC_TDS_PURE:print_debug("TDS_PURE: %f \r\n",vref_value); 
//			break;
//			case ADC_TDS_SOURCE:print_debug("TDS_SOURCE: %f \r\n",vref_value); 
//			break;
//			case ADC_TDS_LEAK:print_debug("TDS_LEAK: %f \r\n",vref_value); 
//			break;
//					
//			case ADC_I_BOOST_PUMP:print_debug("I_BOOST_PUMP: %f \r\n",vref_value); 
//			break;
//			case ADC_I_HEAT_PUMP:print_debug("I_HEAT_PUMP: %f \r\n",vref_value); 
//			break;
//			case ADC_I_NORMAL_PUMP:print_debug("I_NORMAL_PUMP: %f \r\n",vref_value); 
//			break;
//			default:
//				break;
//		}
//	}
	
}
